home *** CD-ROM | disk | FTP | other *** search
- /***
- * thermo.c
- *
- * Main source for Thermometer™.
- *
- * This is based on stuff from an article in Develop magazine vol 6.
- * by Steve Falkenberg
- *
- * Copyright © 1991-1992 Bernard Bernstein. All rights reserved.
- *
- ***/
-
- #if 0
- // this is what my GiveTime func looks like in TCPRoutines.c
- // it can probably go somewhere else, but this hack works.
- Boolean GiveTime(long sleep)
- {
- short ok;
- EventRecord theEvent;
- static delay = 0;
-
- if (!(delay++ % 10))
- SpinCursor(1);
-
- HandleBasicEvent();
-
- return true;
- }
- #endif
-
- #include <string.h>
- #include <MacTCPCommonTypes.h>
- #include <TCPPB.h>
- #include <Color.h>
- #include "CvtAddr.h"
- #include "TCPLow.h"
- #include "TCPRoutines.h"
- #include <CursorCtl.h>
- #include "thermo.h"
-
- /* constants */
-
- #define kBufSize 16384 /* Size for TCP stream buffer and receive buffer */
-
- /* fuction prototypes */
-
- OSErr Temperature(unsigned long ipAddress, unsigned long stream, short *temperature, Boolean retry);
-
- void Initialize();
- void SetupThings();
- void MainEventLoop();
- void CloseThings();
-
-
- extern WindowPtr tempWindow;
- extern WindowPtr errWindow;
- extern Rect dragRect;
- extern Rect thermRect;
- short temperature;
- short origTemp;
- netPrefsRec netPrefs;
- thermPrefsRec thermPrefs;
- unsigned long stream;
- unsigned long ipAddress;
- short quitting = FALSE;
- unsigned long nextTempUpdate = 0L;
- short windowH;
- short windowW;
- Point windowPos;
- Boolean hasWNE;
- RgnHandle mouseRgn;
- CursHandle waitCursor;
- Boolean waiting = FALSE;
-
-
- typedef unsigned char TrapType;
-
- /***
- * TrapAvailable
- *
- * Is the trap available on this machine?
- ***/
- Boolean TrapAvailable(short tNumber, TrapType tType)
- {
- #ifndef _Unimplemented
- #define _Unimplemented 0xA89F
- #endif
-
- return (NGetTrapAddress(tNumber, tType) !=
- GetTrapAddress(_Unimplemented));
- }
-
- /***
- * WNEIsImplemented
- *
- * Is WaitNextEvent available on this machine?
- ***/
- Boolean WNEIsImplemented()
- {
- #ifndef _WaitNextEvent
- #define _WaitNextEvent 0xA860
- #endif
- SysEnvRec theWorld;
-
- SysEnvirons(1, &theWorld);
- if (theWorld.machineType < 0) {
- return(false);
- } else {
- return (TrapAvailable(_WaitNextEvent, ToolTrap));
- }
- }
-
- /***
- * StartWaitCursor
- *
- * Change the cursor to the wait thing.
- ***/
- StartWaitCursor()
- {
- SetCursor(*waitCursor);
- }
-
- /***
- * EndWaitCursor
- *
- * Change the cursor back to normal.
- ***/
- EndWaitCursor()
- {
- InitCursor();
- }
-
- main()
- {
- Initialize();
- SetupThings();
- MainEventLoop();
- CloseThings();
- }
-
- /***
- * Initialize
- *
- * Initialize stuff
- ***/
- void Initialize()
- {
- EventRecord event;
- short count;
- SysEnvRec mac;
- acurHandle waitCurs;
- OSErr err;
-
- MaxApplZone();
-
- InitGraf(&thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- TEInit();
- InitDialogs(NULL);
- InitCursor();
- mouseRgn = NewRgn();
- waitCursor = GetCursor(1000);
- waitCurs = (acurHandle)GetResource('acur', ACURNetWait);
- InitCursorCtl(waitCurs);
-
- SetupErrWindow();
-
- if ((err = InitNetwork()) != noErr) {
- PostAlert(STRInitError, err);
- return;
- }
- }
-
- /***
- * SetupThings
- *
- * Setup default prefs and stuff like that
- ***/
- void SetupThings()
- {
- stream = 0L;
-
- strcpy(netPrefs.name, "thermhost");
- netPrefs.port = 451;
- netPrefs.interval = 10;
- netPrefs.timeout = 10;
- strcpy(netPrefs.search, ": ");
- netPrefs.scale = farenheit;
-
- thermPrefs.min = -10;
- thermPrefs.max = 120;
- thermPrefs.scale = farenheit;
-
- SetUpWindow();
-
- windowPos = topLeft(tempWindow->portRect);
- LocalToGlobal(&windowPos);
-
- windowH = tempWindow->portRect.bottom - tempWindow->portRect.top;
- windowW = tempWindow->portRect.right - tempWindow->portRect.left;
-
- hasWNE = WNEIsImplemented();
- RestorePrefs();
-
- SetUpMenus();
-
- MoveWindow(tempWindow, windowPos.h, windowPos.v, FALSE);
- SizeWindow(tempWindow, windowW, windowH, FALSE);
- ComputeThermRect();
- ShowWindow(tempWindow);
-
- InvalRect(&tempWindow->portRect);
- UpdateWindowTitle();
-
- }
-
- /****
- * HandleMouseDown (theEvent)
- *
- * Take care of mouseDown events.
- *
- ****/
- HandleMouseDown(theEvent)
-
- EventRecord *theEvent;
-
- {
- WindowPtr theWindow;
- short windowCode = FindWindow (theEvent->where, &theWindow);
-
- switch (windowCode)
- {
- case inSysWindow:
- SystemClick (theEvent, theWindow);
- break;
- case inMenuBar:
- AdjustMenus();
- HandleMenu(MenuSelect(theEvent->where));
- break;
- case inDrag:
- DragWindow(theWindow, theEvent->where, &dragRect);
- break;
- case inContent:
- if (theWindow != FrontWindow())
- SelectWindow(theWindow);
- else if (theWindow == tempWindow)
- DoWindowClick(theEvent->where);
- break;
- case inGoAway:
- if (TrackGoAway(theWindow, theEvent->where))
- HideWindow(theWindow);
- break;
- }
- }
- /* end HandleMouseDown */
-
- /****
- * HandleBasicEvent()
- *
- * The main event dispatcher. This routine should be called
- * repeatedly (it handles only one event).
- *
- * This one happens when we are looking for a temperature.
- * This gets called from GiveTime() in TCPRoutines.c
- *
- *****/
- HandleBasicEvent()
- {
- short ok;
- EventRecord theEvent;
- unsigned long secs;
-
- HiliteMenu(0);
-
- if (hasWNE)
- ok = WaitNextEvent(everyEvent, &theEvent, 40L, mouseRgn);
- else {
- SystemTask (); /* Handle desk accessories */
- ok = GetNextEvent (everyEvent, &theEvent);
- }
- waiting = TRUE; /* menus need to know that we are waiting */
- if (ok)
- switch (theEvent.what)
- {
- case mouseDown:
- HandleMouseDown(&theEvent);
- break;
- case keyDown:
- case autoKey:
- if ((theEvent.modifiers & cmdKey) != 0)
- {
- AdjustMenus();
- HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
- }
- break;
- case updateEvt:
- if ((WindowPtr)theEvent.message == tempWindow) {
- BeginUpdate(tempWindow);
- DrawTempWindow(((WindowPeek) tempWindow)->hilited);
- EndUpdate(tempWindow);
- } else if ((WindowPtr)theEvent.message == errWindow) {
- BeginUpdate(errWindow);
- DrawErrWindow(((WindowPeek) errWindow)->hilited);
- EndUpdate(errWindow);
- }
- break;
- case activateEvt:
- if ((WindowPtr)theEvent.message == tempWindow) {
- InvalRect(&tempWindow->portRect);
- } else if ((WindowPtr)theEvent.message == errWindow) {
- InvalRect(&errWindow->portRect);
- }
- break;
- }
- waiting = FALSE;
- }
- /* end HandleBasicEvent */
-
-
- /****
- * HandleEvent()
- *
- * The main event dispatcher. This routine should be called
- * repeatedly (it handles only one event).
- *
- * This one happens normally, when we are not looking for a temperature.
- *
- *****/
- HandleEvent()
- {
- short ok;
- EventRecord theEvent;
- unsigned long secs;
-
- HiliteMenu(0);
-
- if (hasWNE)
- ok = WaitNextEvent(everyEvent, &theEvent, 40L, mouseRgn);
- else {
- SystemTask (); /* Handle desk accessories */
- ok = GetNextEvent (everyEvent, &theEvent);
- }
- if (ok)
- switch (theEvent.what)
- {
- case mouseDown:
- HandleMouseDown(&theEvent);
- break;
- case keyDown:
- case autoKey:
- if ((theEvent.modifiers & cmdKey) != 0)
- {
- AdjustMenus();
- HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
- }
- break;
- case updateEvt:
- if ((WindowPtr)theEvent.message == tempWindow) {
- BeginUpdate(tempWindow);
- DrawTempWindow(((WindowPeek) tempWindow)->hilited);
- EndUpdate(tempWindow);
- } else if ((WindowPtr)theEvent.message == errWindow) {
- BeginUpdate(errWindow);
- DrawErrWindow(((WindowPeek) errWindow)->hilited);
- EndUpdate(errWindow);
- }
- break;
- case activateEvt:
- if ((WindowPtr)theEvent.message == tempWindow) {
- InvalRect(&tempWindow->portRect);
- } else if ((WindowPtr)theEvent.message == errWindow) {
- InvalRect(&errWindow->portRect);
- }
- break;
- }
-
- /* Is it time to update the thermometer? */
- GetDateTime(&secs);
- if (secs > nextTempUpdate) {
- nextTempUpdate = secs + netPrefs.interval * 60;
- tempUpdate();
- }
- }
- /* end HandleEvent */
-
- /***
- * restartConnection
- *
- * Make a new connection. Kill the old one if there was one.
- ***/
- OSErr restartConnection()
- {
- OSErr err;
-
- if (stream != 0L)
- err = ReleaseConnection(stream);
-
- if ((err = CvtAddr(netPrefs.name, &ipAddress)) != noErr) {
- PostAlert(STRAddrError, err);
- return err;
- }
-
- if ((err = CreateStream(&stream, kBufSize)) != noErr) {
- PostAlert(STRStreamError, err);
- return err;
- }
-
- err = Temperature(ipAddress, stream, &temperature, TRUE);
-
- if (netPrefs.scale != thermPrefs.scale) {
- if (thermPrefs.scale == farenheit)
- cToF(&temperature);
- else
- fToC(&temperature);
- }
-
- InvalRect(&thermRect);
-
- return err;
- }
-
- /***
- * MainEventLoop
- *
- * Make boson adjustment to the fibulation unit.
- *:)*/
- void MainEventLoop()
- {
- char hostName[255];
- OSErr err;
-
- StartWaitCursor();
-
- if (stream == 0L)
- err = restartConnection();
-
- while(!quitting)
- HandleEvent();
-
- err = ReleaseConnection(stream);
- SavePrefs();
- DisposeRgn(mouseRgn);
- DisposeErrWindow();
- }
-
- /***
- * tempUpdate
- *
- * update the temperature on the thermometer
- ***/
- tempUpdate()
- {
- OSErr err;
-
- StartWaitCursor();
-
- err = Temperature(ipAddress, stream, &temperature, FALSE);
-
- if (err != noErr) {
- err = ReleaseConnection(stream);
- err = restartConnection();
- }
-
- if (netPrefs.scale != thermPrefs.scale) {
- if (thermPrefs.scale == farenheit)
- cToF(&temperature);
- else
- fToC(&temperature);
- }
-
- InvalRect(&thermRect);
- EndWaitCursor();
- }
-
- /***
- * cToF
- *
- * Celcius to Fahrenheit converter
- ***/
- cToF(short *temp)
- {
- short newTemp;
- newTemp = *temp * (9.0 / 5) + 32;
- *temp = newTemp;
- }
-
- /***
- * fToC
- *
- * Fahrenheit to Celcius converter
- ***/
- fToC(short *temp)
- {
- short newTemp;
- newTemp = (*temp - 32) * (5.0 / 9);
- *temp = newTemp;
- }
-
- /***
- * CloseThings
- *
- * Close things (hmmm, I guess not)
- ***/
- void CloseThings()
- {
- }
-
- /***
- * Temperature
- *
- * Read the temperature from the host
- ***/
- OSErr Temperature(unsigned long ipAddress, unsigned long stream, short *temperature, Boolean retry)
- {
- OSErr err;
- long bufOffset = 0;
- unsigned short dataLength;
- Ptr data;
- Handle tempData;
- char *c;
- char numStr[256];
- short numLen;
- short loc;
- long tmpTemp;
-
- PostAlert(STRGetting, 0);
-
- if ((err = OpenConnection(stream,ipAddress,netPrefs.port,netPrefs.timeout)) != noErr) {
- if (retry) PostAlert(STROpenError, err);
- *temperature = origTemp;
- return err;
- }
-
- tempData = NewHandle(kBufSize);
- if (tempData == NULL) {
- *temperature = origTemp;
- CloseConnection(stream);
- HandleError(memFullErr);
- return memFullErr;
- }
-
- HLock(tempData);
- data = *tempData;
- dataLength = kBufSize;
-
- while ((err = RecvData(stream,data,&dataLength,false)) == noErr) {
- bufOffset += dataLength;
- dataLength = kBufSize;
- HUnlock(tempData);
- SetHandleSize(tempData,bufOffset+kBufSize);
- HLock(tempData);
- data = *tempData + bufOffset;
- }
-
- data[0] = '\0';
-
- HUnlock(tempData);
- if (err == connectionClosing)
- err = noErr;
-
- if (retry) HandleError(err);
-
- CloseConnection(stream);
-
- HLock(tempData);
-
- /* Parse the string for the temperature */
- loc = find(netPrefs.search, *tempData);
- if (loc < strlen(*tempData)) {
- c = *tempData + loc;
- numLen = strcspn(c, "\n\t\r ");
- strncpy(numStr, c, numLen);
- numStr[numLen] = '\0';
- CtoPstr(numStr);
- StringToNum(numStr, &tmpTemp);
- origTemp = (short)tmpTemp;
- } else {
- PostAlert(STRParseError, 0);
- }
-
- HUnlock(tempData);
- DisposHandle(tempData);
-
- *temperature = origTemp;
-
- if (err == noErr)
- PostAlert(STRTempOK, 0);
- return err;
- }
-
- /***
- * find
- *
- * find the given string in the source, return the offset
- ***/
- short find(char *string, char *source)
- {
- short i, j, length, slen, match[20], matched, found, pos;
- char cc;
-
- length = strlen(string);
- match[0] = 0;
- for (i = 1; i < length; i++)
- {
- matched = 0;
- match[i] = 0;
- while (string[i + matched] == string[matched]) matched++;
- for (j = 0; j < matched; j++) match[i + j] = j + 1;
- if (matched) i = i + matched - 1;
- }
-
- slen = strlen(source);
- found = 0;
- matched = 0;
- pos = 0;
- while ((pos < slen) && (!(found)))
- {
- cc = source[pos];
- pos++;
- if (cc == string[matched])
- {
- matched++;
- if (matched == length) found = 1;
- }
- else
- {
- if (matched != 0) pos--;
- while ((matched != 0) && (cc != string[matched]))
- matched = match[matched - 1];
- }
- }
-
- if (found) return(pos);
- else return(slen);
- }
-
- /***
- * doAbout
- *
- * display the about dlog
- ***/
- doAbout()
- {
- DialogPtr theDlg;
- short theItem;
-
- theDlg = GetNewDialog(DLOGAbout, NULL, -1L);
- ModalDialog(NULL, &theItem);
- DisposDialog(theDlg);
- }
-
- /***
- * changeThermPrefs
- *
- * Change the thermometer prefs: range, C/F
- * just redraw the thermometer with done
- ***/
- changeThermPrefs()
- {
- if (getThermPrefs(&thermPrefs)) {
- tempUpdate();
- InvalRect(&tempWindow->portRect);
- }
- }
-
- /***
- * changeNetPrefs
- *
- * Change the network prefs: host addr, port, etc.
- * restart connection when done
- ***/
- changeNetPrefs()
- {
- if (getNetPrefs(&netPrefs)) {
- StartWaitCursor();
- restartConnection();
- EndWaitCursor();
- InvalRect(&tempWindow->portRect);
- }
- }
-
- /***
- * getNetPrefs
- *
- * Get the net prefs from the dialog
- ***/
- getNetPrefs(netPrefsRec *prefs)
- {
- DialogPtr dlg;
- short item;
- Boolean done = FALSE;
- Boolean changed;
-
- dlg = GetNewDialog(DLOGNetwork, NULL, -1L);
- SetupNetItems(dlg, prefs);
- do {
- ModalDialog(NULL, &item);
- switch(item) {
- case DItmOK:
- FillNetPrefs(dlg, prefs);
- done = TRUE;
- changed = TRUE;
- break;
- case DItmCancel:
- done = TRUE;
- changed = FALSE;
- break;
- case DItmNCel:
- CheckForC(dlg, DItmNCel, DItmNFah, celcius);
- break;
- case DItmNFah:
- CheckForC(dlg, DItmNCel, DItmNFah, farenheit);
- break;
- }
- } while (!done);
- DisposDialog(dlg);
- return changed;
- }
-
- /***
- * FillNetPrefs
- *
- * Fill the record with stuff from the dlog
- ***/
- FillNetPrefs(DialogPtr dlg, netPrefsRec *prefs)
- {
- short itemType;
- Handle itemHdl;
- Rect itemBox;
- Str255 tmpString;
- long tmpNum;
-
- GetDItem(dlg, DItmAddr, &itemType, &itemHdl, &itemBox);
- GetIText(itemHdl, &tmpString);
- PtoCstr((char*)tmpString);
- strcpy(prefs->name, (char*)tmpString);
-
- GetDItem(dlg, DItmPort, &itemType, &itemHdl, &itemBox);
- GetIText(itemHdl, &tmpString);
- StringToNum(tmpString, &tmpNum);
- prefs->port = (short)tmpNum;
-
- GetDItem(dlg, DItmTimeout, &itemType, &itemHdl, &itemBox);
- GetIText(itemHdl, &tmpString);
- StringToNum(tmpString, &tmpNum);
- prefs->timeout = (short)tmpNum;
-
- GetDItem(dlg, DItmTime, &itemType, &itemHdl, &itemBox);
- GetIText(itemHdl, &tmpString);
- StringToNum(tmpString, &tmpNum);
- prefs->interval = (short)tmpNum;
-
- GetDItem(dlg, DItmSearch, &itemType, &itemHdl, &itemBox);
- GetIText(itemHdl, &tmpString);
- PtoCstr((char*)tmpString);
- strcpy(prefs->search, (char*)tmpString);
-
- GetForC(dlg, DItmNCel, DItmNFah, &prefs->scale);
- }
-
- /***
- * SetupNetItems
- *
- * Setup the net prefs dlog from the rec
- ***/
- SetupNetItems(DialogPtr dlg, netPrefsRec *prefs)
- {
- short itemType;
- Handle itemHdl;
- Rect itemBox;
- Str255 tmpString;
-
- strcpy((char*)tmpString, prefs->name);
- CtoPstr((char*)tmpString);
- GetDItem(dlg, DItmAddr, &itemType, &itemHdl, &itemBox);
- SetIText(itemHdl, tmpString);
-
- NumToString((long)prefs->port, tmpString);
- GetDItem(dlg, DItmPort, &itemType, &itemHdl, &itemBox);
- SetIText(itemHdl, tmpString);
-
- NumToString((long)prefs->interval, tmpString);
- GetDItem(dlg, DItmTime, &itemType, &itemHdl, &itemBox);
- SetIText(itemHdl, tmpString);
-
- NumToString((long)prefs->timeout, tmpString);
- GetDItem(dlg, DItmTimeout, &itemType, &itemHdl, &itemBox);
- SetIText(itemHdl, tmpString);
-
- strcpy((char*)tmpString, prefs->search);
- CtoPstr((char*)tmpString);
- GetDItem(dlg, DItmSearch, &itemType, &itemHdl, &itemBox);
- SetIText(itemHdl, tmpString);
-
- CheckForC(dlg, DItmNCel, DItmNFah, prefs->scale);
-
- SelIText(dlg, DItmAddr, 0, 32767);
- }
-
- /***
- * getThermPrefs
- *
- * Get the thermo prefs from the dialog
- ***/
- getThermPrefs(thermPrefsRec *prefs)
- {
- DialogPtr dlg;
- short item;
- Boolean done = FALSE;
- Boolean changed;
-
- dlg = GetNewDialog(DLOGTherm, NULL, -1L);
- SetupThermItems(dlg, prefs);
- do {
- ModalDialog(NULL, &item);
- switch(item) {
- case DItmOK:
- FillThermPrefs(dlg, prefs);
- done = TRUE;
- changed = TRUE;
- break;
- case DItmCancel:
- done = TRUE;
- changed = FALSE;
- break;
- case DItmTCel:
- CheckForC(dlg, DItmTCel, DItmTFah, celcius);
- break;
- case DItmTFah:
- CheckForC(dlg, DItmTCel, DItmTFah, farenheit);
- break;
- }
- } while (!done);
- DisposDialog(dlg);
- return changed;
- }
-
- /***
- * FillThermPrefs
- *
- * Fill the therm prefs record from the dialog
- ***/
- FillThermPrefs(DialogPtr dlg, thermPrefsRec *prefs)
- {
- short itemType;
- Handle itemHdl;
- Rect itemBox;
- Str255 tmpString;
- long tmpNum;
-
- GetDItem(dlg, DItmMin, &itemType, &itemHdl, &itemBox);
- GetIText(itemHdl, &tmpString);
- StringToNum(tmpString, &tmpNum);
- prefs->min = (short) tmpNum;
-
- GetDItem(dlg, DItmMax, &itemType, &itemHdl, &itemBox);
- GetIText(itemHdl, &tmpString);
- StringToNum(tmpString, &tmpNum);
- prefs->max = (short) tmpNum;
-
- GetForC(dlg, DItmTCel, DItmTFah, &prefs->scale);
- UpdateWindowTitle();
-
- }
-
- /***
- * SetupThermItems
- *
- * Fill the therm prefs dlog from the rec
- ***/
- SetupThermItems(DialogPtr dlg, thermPrefsRec *prefs)
- {
- short itemType;
- Handle itemHdl;
- Rect itemBox;
- Str255 tmpString;
-
- NumToString((long)prefs->min, tmpString);
- GetDItem(dlg, DItmMin, &itemType, &itemHdl, &itemBox);
- SetIText(itemHdl, tmpString);
-
- NumToString((long)prefs->max, tmpString);
- GetDItem(dlg, DItmMax, &itemType, &itemHdl, &itemBox);
- SetIText(itemHdl, tmpString);
-
- CheckForC(dlg, DItmTCel, DItmTFah, prefs->scale);
-
- SelIText(dlg, DItmMin, 0, 32767);
- }
-
- /***
- * CheckForC
- *
- * That is Check F or C (fahrenheit or celcius) (radio buttons)
- ***/
- CheckForC(DialogPtr dlg, short cItem, short fItem, ForC scale)
- {
- short itemType;
- Handle itemHdl;
- Rect itemBox;
-
- GetDItem(dlg, cItem, &itemType, &itemHdl, &itemBox);
- SetCtlValue(itemHdl, scale == celcius);
-
- GetDItem(dlg, fItem, &itemType, &itemHdl, &itemBox);
- SetCtlValue(itemHdl, scale == farenheit);
- }
-
-
- /***
- * GetForC
- *
- * That is Get F or C (fahrenheit or celcius) (radio buttons)
- ***/
- GetForC(DialogPtr dlg, short cItem, short fItem, ForC *scale)
- {
- short itemType;
- Handle itemHdl;
- Rect itemBox;
- Boolean isThisOne;
-
- GetDItem(dlg, cItem, &itemType, &itemHdl, &itemBox);
- if (GetCtlValue(itemHdl))
- *scale = celcius;
- else
- *scale = farenheit;
-
- }
-
- /***
- * SavePrefs
- *
- * Save prefs file with net and thermo prefs
- ***/
- SavePrefs()
- {
- StringHandle fileHdl;
- StringHandle folderName;
- OSErr err;
- SysEnvRec envRec;
- short refNum;
- short volNum;
- long dirID;
- long procID;
- Str255 fileName;
- prefsFileRec prefRec;
- long count;
- GrafPtr savePort;
- short sVolNum;
- long sDirID, sProcID;
- CInfoPBRec pb;
-
-
- GetPort(&savePort);
- SetPort(tempWindow);
-
- windowPos = topLeft(tempWindow->portRect);
- windowH = tempWindow->portRect.bottom - tempWindow->portRect.top;
- windowW = tempWindow->portRect.right - tempWindow->portRect.left;
- LocalToGlobal(&windowPos);
-
- SysEnvirons(1, &envRec);
-
- folderName = GetString(STRPrefsFolder);
- if (folderName == NULL) {
- HandleError(ResError());
- return;
- }
- err = GetWDInfo(envRec.sysVRefNum, &volNum, &sDirID, &sProcID);
- if (err != noErr) {
- HandleError(err);
- return;
- }
- pb.dirInfo.ioCompletion = NULL;
- pb.dirInfo.ioNamePtr = *folderName;
- pb.dirInfo.ioVRefNum = volNum;
- pb.dirInfo.ioFDirIndex = 0;
- pb.dirInfo.ioDrDirID = sDirID;
- err = PBGetCatInfo(&pb, FALSE);
- if (err == fnfErr) {
- err = DirCreate(sVolNum, sDirID, *folderName, &dirID);
- } else if (err == noErr) {
- dirID = pb.dirInfo.ioDrDirID;
- } else {
- HandleError(err);
- return;
- }
-
-
- fileHdl = GetString(STRPrefsName);
- if (fileHdl == NULL) {
- HandleError(ResError());
- return;
- }
-
- BlockMove(*fileHdl, fileName, *fileHdl[0] + 1);
-
- err = HOpen(volNum, dirID, fileName, fsWrPerm, &refNum);
- if (err == fnfErr) {
- err = HCreate(volNum, dirID, fileName, CREATOR, PREFTYPE);
- err = HOpen(volNum, dirID, fileName, fsWrPerm, &refNum);
- if (err != noErr) {
- HandleError(err);
- return;
- }
- } else if (err != noErr) {
- HandleError(err);
- return;
- }
-
- prefRec.version = PREFVERSION;
- BlockMove(&netPrefs, &prefRec.netPrefs, sizeof(netPrefs));
- BlockMove(&thermPrefs, &prefRec.thermPrefs, sizeof(thermPrefs));
- prefRec.windowPos = windowPos;
- prefRec.width = windowW;
- prefRec.height = windowH;
- count = sizeof(prefRec);
- err = FSWrite(refNum, &count, &prefRec);
- err = FSClose(refNum);
- SetPort(savePort);
- }
-
-
- /***
- * RestorePrefs
- *
- * Read prefs from the prefs file and file records
- ***/
- RestorePrefs()
- {
- StringHandle strHdl;
- StringHandle folderName;
- OSErr err;
- SysEnvRec envRec;
- short refNum;
- short volNum;
- long dirID;
- long procID;
- long sDirID, sProcID;
- Str255 fileName;
- prefsFileRec prefRec;
- long count;
- RgnHandle rgn;
- Rect windowRect;
- CInfoPBRec pb;
-
-
- SysEnvirons(1, &envRec);
-
- folderName = GetString(STRPrefsFolder);
- if (folderName == NULL) {
- HandleError(ResError());
- return;
- }
- err = GetWDInfo(envRec.sysVRefNum, &volNum, &sDirID, &sProcID);
- if (err != noErr) {
- HandleError(err);
- return;
- }
- pb.dirInfo.ioCompletion = NULL;
- pb.dirInfo.ioNamePtr = *folderName;
- pb.dirInfo.ioVRefNum = volNum;
- pb.dirInfo.ioFDirIndex = 0;
- pb.dirInfo.ioDrDirID = sDirID;
- err = PBGetCatInfo(&pb, FALSE);
- if (err == fnfErr) {
- err = DirCreate(volNum, sDirID, *folderName, &dirID);
- } else if (err == noErr) {
- dirID = pb.dirInfo.ioDrDirID;
- } else {
- HandleError(err);
- return;
- }
-
- strHdl = GetString(STRPrefsName);
- if (strHdl == NULL) {
- HandleError(ResError());
- return;
- }
-
- BlockMove(*strHdl, fileName, *strHdl[0] + 1);
-
- err = HOpen(volNum, dirID, fileName, fsWrPerm, &refNum);
- if (err == fnfErr) {
- changeNetPrefs();
- return;
- } else if (err != noErr) {
- HandleError(err);
- return;
- }
-
- count = sizeof(prefRec);
- err = FSRead(refNum, &count, &prefRec);
- err = FSClose(refNum);
-
- if (prefRec.version != PREFVERSION)
- return;
-
- BlockMove(&prefRec.netPrefs, &netPrefs, sizeof(netPrefs));
- BlockMove(&prefRec.thermPrefs, &thermPrefs, sizeof(thermPrefs));
-
- topLeft(windowRect) = prefRec.windowPos;
- windowRect.right = windowRect.left + prefRec.width;
- windowRect.bottom = windowRect.top + prefRec.height;
-
- rgn = GetGrayRgn();
- if (RectInRgn(&windowRect, rgn)) {
- windowPos = prefRec.windowPos;
- windowH = prefRec.height;
- windowW = prefRec.width;
- }
- }
-
- /***
- * HandleError
- *
- * Post the error alert
- ***/
- HandleError(OSErr err)
- {
- short errStringID;
-
- if (err != noErr) {
- if (err <= -23000 && err >= -23048)
- errStringID = STRNetError;
- else
- errStringID = STRAnyError;
- PostAlert(errStringID, err);
- }
- }
-
- /***
- * PostAlert
- *
- * This used to display an alert, but now it just updates the message
- * window. Who cares if the temperature did not get updated recently?
- * You can always look at the status window to see if it is current.
- ***/
- PostAlert(short stringID, OSErr errNum)
- {
- Str255 errString;
- unsigned char errNumString[30];
- unsigned char timeString[30];
- unsigned char dateString[128];
- long seconds;
-
- GetIndString(errString, STRErrors, stringID);
-
- DisplayMessage(errString);
- }
-